<template>
	<view class="">
		<view class="mask">
			<view class="content">
				<button class="login-button" @click="openPopup">
					<uni-icons custom-prefix="custom-icon" type="weixin" size="40"></uni-icons>
					微信一键登录</button>
				<uni-icons custom-prefix="custom-icon" type="close" size="60" class="clossIcon"
					@click="close"></uni-icons>
			</view>
		</view>
		<van-popup :show="show" position="bottom" custom-style="height: 50%;" @close="onClose">
			<view class="popup_view">
				<button class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
					<image class="avatar" :src="userInfo.avatarUrl"></image>
				</button>
				<div class="nickname_div">
					<label for="nickname">昵称：</label>
					<input type="nickname" v-model="userInfo.nickname" name="nickname" @change="onInput" placeholder="请输入昵称"/>
				</div>
				<van-button type="primary" @click="ulogin()" size="large"
					style="width:70%; margin-top: 40rpx;margin-bottom: 20rpx;">提交</van-button>
			</view>
		</van-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				userInfo: {
					avatarUrl: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
					nickname: "",
					openid: uni.getStorageSync('openid')
				}
			}
		},
		methods: {
			ulogin() {
				let api = this.$Api;
				//前端数据请求时，显示加载提示弹框
				uni.showLoading({
					title: '加载中...',
					mask: true,
				});
				api.login(this.userInfo).then(res => {
					console.log(res);
					uni.setStorageSync('uid', res.data.data.uid);
					uni.setStorageSync('token', res.data.data.token);
					this.goPersonalInfo();
					uni.hideLoading();
				}).catch(err => {
					console.log(err);
				});
			},

			goPersonalInfo() {
				uni.switchTab({
					url: '../personal/index',
					success: () => {
						console.log(1);
					},
					fail: (res) => {
						console.log('navigate failed', res);
					}
				})
			},

			close() {
				uni.navigateBack();
			},
			onClose() {
				this.show = false;
			},
			openPopup() {
				this.show = true;
			},
			onChooseAvatar(e) {
				this.userInfo.avatarUrl = e.detail?.avatarUrl;
			},
			onInput(e) {
				this.userInfo.nickname = e.detail?.value;
			}
		}
	}
</script>

<style>
	page {
		background: transparent;
	}

	.mask {
		position: fixed;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: center;
		align-items: center;
		background-color: rgba(0, 0, 0, 0.0);
	}

	.content {
		height: 100%;
		width: 100%;
		background-color: #c7c9ce;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.login-button {
		background-color: #18bc37;
		width: 60%;
		height: 100rpx;
		font-size: 40rpx;
		font-weight: 700;
		border-radius: 40rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
	}

	.clossIcon {
		margin-top: 100rpx;
	}
	
	.popup_view{
		height: 100%;
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	.avatar-wrapper {
		padding: 0;
		width: 80px;
		height: 80px;
		border-radius: 8px;
		background-color: #fff;
		margin-bottom: 10px;
	}

	.avatar {
		width: 100%;
		height: 100%;
	}
	
	.nickname_div{
		display: flex;
	}
</style>